package cn.udrm.elasticsearch.demo;

import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class DemoThread {
    private static List list=new CopyOnWriteArrayList();

    private static int threadConuts=5;

    private static  int subThreadCounts=10000;

    public static void main(String[] args) throws InterruptedException {
        ExecutorService service= Executors.newFixedThreadPool(threadConuts);
        ExecutorService subService= Executors.newFixedThreadPool(subThreadCounts);
        for(int i=0;i<threadConuts;i++){
            service.submit(new Runnable() {
                @Override
                public void run() {
                    testSubThread(subService);
                }
            });
        }
        service.shutdown();
        boolean flag=true;
        while(flag){
            Thread.sleep(1000);
            if(service.isTerminated()){
                subService.shutdown();
                flag=false;
                boolean subFlag=true;
                while(subFlag){
                    System.out.println("=======Size："+list.size());
                    if(subService.isTerminated()){
                        subFlag=false;
                    }
                }
            }
        }
        //subService.shutdown();
    }

    public static void testSubThread(ExecutorService service){
        for(int i=0;i<subThreadCounts;i++){
            final int k=i;
            service.submit(new Runnable() {
                @Override
                public void run() {
                    list.add(0);
                    //System.out.println("==========子线程输出："+(list.size())+" Thread："+Thread.currentThread().getName());
                }
            });
        }
    }
}
